package com.guoxin.concurrentprogramming.chapter2;

/**
 * @author guoxin
 * @date 2019/7/15
 */
public class BadBlockOnInteger implements Runnable {
    public static Integer i = 0;

    private static final Object OBJ = new Object();
    @Override
    public void run() {
        for (int j = 0; j < 1000000; j++) {
            synchronized (i) {
                //i++本质是创建一个新的Integer对象，并将它的引用赋值给i
                i++;
            }
        }
    }


    public static void main(String[] args) throws InterruptedException {
        BadBlockOnInteger b = new BadBlockOnInteger();
        Thread t1 = new Thread(b);
        Thread t2 = new Thread(b);

        t1.start();
        t2.start();

        t1.join();
        t2.join();

        System.out.println(i);
    }
}
